package main

import (
	"bufio"
	"encoding/json"
	"fmt"
	"os"
)

type Person struct {

	Id int64 `json:"id"` // 用户标识

	Stature float64 // 身高

	Weight float64 // 体重

	Bmi float64 // bmi 计算值
	// bmi := weight / (stature * stature)

	Gender string // 性别

	GenderType int // 性别类型

	Age int32 // 年龄

	FatRate float64 // 体脂率
	// 男 genderType = 1  女 genderType = 0
	// fatRate := 1.2 * bmi + 0.23 * float64(age) - 5.4 - 10.8 * float64(genderType)

}

type FatRateRank struct {

	PersonMap map[int64]*Person



}

func CreatFatRateRank() *FatRateRank {
	fatRateRank := new(FatRateRank)
	personMap := make(map[int64]*Person)
	fatRateRank.PersonMap = personMap
	return fatRateRank
}

func RegisterPerson(id int64, stature float64, weight float64, gender string, age int) *Person {
	person := new(Person)
	person.Id = id
	person.Stature = stature
	person.Weight = weight

	bmi := weight / (stature * stature)
	person.Bmi = bmi

	person.Gender = gender
	genderType := 0
	if "b" == gender || "boy" == gender {
		genderType = 1
	} else if "g" == gender || "girl" == gender {
		genderType = 0
	}
	person.GenderType = genderType

	fatRate := 1.2 * bmi + 0.23 * float64(age) - 5.4 - 10.8 * float64(genderType)
	person.FatRate = fatRate

	fmt.Println("创建用户成功 用户标识:", person.Id)
	person.jsonPersonSave()

	// 排序
	return person
}




func (person *Person) jsonPersonSave() {
	jsonData, _ := json.Marshal(person)
	jsonString := string(jsonData)
	fmt.Println("用户转JSON:", jsonString)
	// 用户 JSON 存入文件
	file, _ := os.Create("rank.json.txt")
	write := bufio.NewWriter(file)
	write.WriteString(jsonString)
	write.Flush()
}

func (person *Person) updatePersonStature(stature float64) {
	person.Stature = stature
}

func (person *Person) updatePersonWeigth(weight float64) {
	person.Weight = weight
}

func (person *Person) updatePersonAge(age int32) {
	person.Age = age
}

func (fatRateRank *FatRateRank) PersonAddRank(person *Person) {
	fatRateRank.PersonMap[person.Id] = person
}

func (fatRateRank *FatRateRank) GetFatRateRank() {
	// size := len(fatRateRank.PersonMap)
	// personSlice := make([]Person, size)
	// sort.Slice(personSlice)
}

func (fatRateRank *FatRateRank) GetPersonFateRank(personId int64) {
	
}

func (fatRateRank *FatRateRank) GetPerson(personId int64) *Person {
	person := fatRateRank.PersonMap[personId]
	return person
}

// 排序


type PersonSortSlice []Person

func (personSortSlice PersonSortSlice) Len() int {
	return len(personSortSlice)
}

func (personSortSlice PersonSortSlice) Swap(p1 int, p2 int) {
	personSortSlice[p1], personSortSlice[p2] = personSortSlice[p2], personSortSlice[p1]
}

func (personSortSlice PersonSortSlice) Less(p1 int, p2 int) bool {
	return personSortSlice[p1].FatRate > personSortSlice[p2].FatRate
}

func main() {
	fmt.Println("第五次作业 面向对象 体脂排行榜")

	// 创建排行榜
	fatRateRank := CreatFatRateRank()


	person1 := RegisterPerson(1001, 45.12, 33.12, "boy", 33)
	fmt.Println("注册用户:", person1.Id)

	fatRateRank.PersonAddRank(person1)


}

